Skip to content

Add experimental NamedTuple copyFrom method #23135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bishabosha
Copy link
Member

copyFrom chosen as it has a slight difference i.e. the argument should be surrounded in extra parentheses. Also, it can not be used for target typing of fields.

`copyFrom` chosen as it has a slight difference i.e. the argument should be surrounded in extra parentheses.
Also, it can not be used for target typing of fields.
@bishabosha
Copy link
Member Author

rebased


@main def Test =
val foo: Foo = (name = "foo", mod = "some_mod")
val foo_updated: Foo = foo.copyFrom((mod = "bar")) // error, stays as String
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is a demo of the limitation of no target typing, which I guess could be mitigated with a dedicated feature, or a secondary library solution based on lenses

@Ichoran
Copy link

Ichoran commented May 17, 2025

For reference, with the help you provided, I got this working in my code, including in an optimized form for the special-cased tuple sizes (up to 22): https://github.com/Ichoran/kse3/blob/fef8e9b971861306c5da30f12d868294f53b410f/basics/src/Labels.scala#L447-L466

It seems to work pretty well. If you want to not allow type changes, I'm sure one could do that type computation too--you're already matching the types of the labels, after all.

(I did lenses too, but I didn't see much use for restricting types, so I didn't bother putting that into them, just regular map, delete, insert, etc.. Lensing by name-as-a-string-constant is kind of a neat trick. One doesn't even need macros at this point!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants